package org.android.kumoh_ing;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;


public class LocationDBTransfer {
	
	public String setLocation(String u_id,String u_latitude,String u_longitude) throws ClientProtocolException, IOException {
 		// TODO Auto-generated method stub
		HttpPost request = makeHttpPost(u_id,u_latitude,u_longitude,"http://14.63.225.125/setLocation.php");
		HttpClient client = new DefaultHttpClient();
		ResponseHandler<String> reshandler = new BasicResponseHandler();
		String result = client.execute(request, reshandler);
		return result;
	}
	public String setFeedLocation(String feed_id,String latitude,String longitude) throws ClientProtocolException, IOException {
 		// TODO Auto-generated method stub
		HttpPost request = makeHttpfeedPost(feed_id, latitude, longitude,"http://14.63.225.125/setFeedLocation.php");
		HttpClient client = new DefaultHttpClient();
		ResponseHandler<String> reshandler = new BasicResponseHandler();
		String result = client.execute(request, reshandler);
		return result;
	}
	
	public String setPermission(String u_id, String u_position) throws ClientProtocolException,IOException {
		HttpPost request = makeHttpPost(u_id, u_position, "http://14.63.225.125/setPermission.php");
		HttpClient client = new DefaultHttpClient();
		ResponseHandler<String> reshandler = new BasicResponseHandler();
		String result = client.execute(request, reshandler);
		return result;
	}
	public String getLocation(String u_id) throws ClientProtocolException,IOException {
		// TODO Auto-generated method stub
		HttpPost request = makeHttpPost(u_id,"http://14.63.225.125/getLocation.php");
		HttpClient client = new DefaultHttpClient();
		ResponseHandler<String> reshandler = new BasicResponseHandler();
		String result = client.execute(request, reshandler);
		
		return result;
	}
	public String getFeedLocation(String feed_id) throws ClientProtocolException,IOException {
		// TODO Auto-generated method stub
		HttpPost request = makeHttpFeedPost(feed_id,"http://14.63.225.125/getFeedLocation.php");
		HttpClient client = new DefaultHttpClient();
		ResponseHandler<String> reshandler = new BasicResponseHandler();
		String result = client.execute(request, reshandler);
		
		return result;
	}
	public String getPermission(String u_id) throws ClientProtocolException,IOException {
		HttpPost request = makeHttpPost(u_id,"http://14.63.225.125/getPermission.php");
		HttpClient client = new DefaultHttpClient();
		ResponseHandler<String> reshandler = new BasicResponseHandler();
		String result = client.execute(request, reshandler);
		return result;
	}
	
	
	private HttpPost makeHttpPost(String $u_id, String $url) {

		// TODO Auto-generated method stub

		HttpPost request = new HttpPost($url);

		ArrayList<NameValuePair> nameValue = new ArrayList<NameValuePair>();
		
		nameValue.add(new BasicNameValuePair("u_id", $u_id));
		request.setEntity(makeEntity(nameValue));
		return request;
		 
	}
	private HttpPost makeHttpFeedPost(String $feed_id, String $url) {

		// TODO Auto-generated method stub

		HttpPost request = new HttpPost($url);

		ArrayList<NameValuePair> nameValue = new ArrayList<NameValuePair>();
		
		nameValue.add(new BasicNameValuePair("feed_id", $feed_id));
		request.setEntity(makeEntity(nameValue));
		return request;
		 
	}
	private HttpPost makeHttpPost(String $u_id, String $u_position, String $url) {

		// TODO Auto-generated method stub

		HttpPost request = new HttpPost($url);

		ArrayList<NameValuePair> nameValue = new ArrayList<NameValuePair>();
		
		nameValue.add(new BasicNameValuePair("u_id", $u_id));
		nameValue.add(new BasicNameValuePair("u_position", $u_position));
		
		request.setEntity(makeEntity(nameValue));
		return request;
		 
	}
	
	private HttpPost makeHttpPost(String $u_id, String $u_latitude, String $u_longitude, String $url) {

		// TODO Auto-generated method stub

		HttpPost request = new HttpPost($url);

		ArrayList<NameValuePair> nameValue = new ArrayList<NameValuePair>();

		nameValue.add(new BasicNameValuePair("u_id", $u_id));
		nameValue.add( new BasicNameValuePair("u_latitude", $u_latitude ));
		nameValue.add( new BasicNameValuePair("u_longitude", $u_longitude ));
		request.setEntity(makeEntity(nameValue));
		return request;
	}
	private HttpPost makeHttpfeedPost(String $feed_id, String $latitude, String $longitude, String $url) {
		// TODO Auto-generated method stub

		HttpPost request = new HttpPost($url);

		ArrayList<NameValuePair> nameValue = new ArrayList<NameValuePair>();

		nameValue.add(new BasicNameValuePair("feed_id", $feed_id));
		nameValue.add( new BasicNameValuePair("latitude", $latitude ));
		nameValue.add( new BasicNameValuePair("longitude", $longitude ));
		request.setEntity(makeEntity(nameValue));
		return request;
	}

	private HttpEntity makeEntity(ArrayList<NameValuePair> $nameValue) {

		HttpEntity result = null;

		try {

			result = new UrlEncodedFormEntity($nameValue);

		} catch (UnsupportedEncodingException e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

		}
		return result;
	}
}
